package com.banca2.bracketplant.communicate;

import android.content.Context;
import android.net.ConnectivityManager;
import android.util.Log;

import com.banca2.bracketplant.common.RequestType;
import com.banca2.bracketplant.pojo.BncRequest;

public class CommunicationManager {

	private static final String LOG_TAG = CommunicationManager.class.getSimpleName();
	
	/**
	 * 检测网络状况
	 * 
	 * @param context
	 * @return
	 * 
	 * @author ZQ(上午2:48:48)
	 */
	public static boolean checkNetState(Context context) {
		ConnectivityManager connManager = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);

		boolean result = connManager.getActiveNetworkInfo() != null
				&& connManager.getActiveNetworkInfo().isAvailable();

		if (result) {
			Log.d(LOG_TAG, "网络访问正常");
		} else {
			Log.d(LOG_TAG, "网络无法访问");
		}
		return result;
	}// checkNetState

	/**
	 * 执行网络通讯请求
	 * @param isPost 方法类型：true-POST；false-GET
	 * @param requestType 不允许为null，请求的类型，即URL的“文件名”部分
	 * @param params 请求参数
	 * @param processor 处理服务器响应的回调对象
	 *
	 * @author ZQ(下午6:18:05)
	 */
	public static void doRequest(boolean isPost, RequestType requestType, Object params, BcnResponseProcessor processor){
		// TODO 生产者消费者模式，控制通讯线程数，考虑使用executeOnExecutor
		Log.v(LOG_TAG, "doRequest(boolean, RequestType, Object, ResponseProcessor) - start ");
		if( null != requestType ){
			new CommunicationAsyncTask().execute(new BncRequest(isPost, requestType, params, processor)) ;
		} else {
			Log.e(LOG_TAG, "null == requestType");
			throw new RuntimeException("null == requestType");
		}
		Log.v(LOG_TAG, "doRequest(boolean, RequestType, Object, ResponseProcessor) - end ");
	}//doRequest
}
